enum BTN {
    //% block="K1"
    1,
    //% block="K2"
    2,
    //% block="K3"
    3
	//%block="K4"
	4
}
enum MOTOR {
    //% block="A"
    0,
    //% block="B"
    1,
    //% block="C"
    2
	//%block="D"
	3
}
enum DIG
{
	//% block="IO4"
	4,
	//% block="IO16"
	16,
	//% block="IO17"
	17,
	//%block="IO14"
	14,
	//% block="IO12"
	12,
}
enum COLORS
{
	//% block="黑"
	1,
	//% block="白"
	0,
}
enum BMDJ
{
	//% block="C"
	0,
	//% block="D"
	1,
}
enum LIGHTS
{
	//% block="左"
	0,
	//% block="右"
	1,
}
enum WULUXUNJI
{
	//% block="1"
	1,
	//% block="2"
	2,
	//% block="3"
	3,
	//% block="4"
	4,
	//% block="5"
	5	
}
enum HX6050
{
	//% block="X角度"
	1,
	//% block="Y角度"
	2,
	//% block="Z角度"
	3
}
//% color="#0080FF" iconWidth=50 iconHeight=40
namespace HXSY {
		  
		//% block="初始化陀螺仪传感器" blockType="command"
		export function HXMPU1(parameter: any, block: any) {
		  			Generator.addInclude('MPU6050HX1', '#include <MPU6050_HX.h>');
		  			Generator.addInclude('MPU6050HX2', '#include <Wire.h>');
		  			Generator.addSetup('MPU6050HX3', "Wire.begin();");
		  			Generator.addSetup('MPU6050HX4', "mpu6050.begin();");
		  			Generator.addSetup('MPU6050HX6', "mpu6050.calcGyroOffsets(true);");
		  			Generator.addObject(`MPU6050HX5`, `MPU6050`, `mpu6050(Wire);`);
		  }
			
		//% block="获取陀螺仪传感器的[MP6]" blockType="reporter"
		//% MP6.shadow="dropdown" MP6.options="HX6050" MP6.defl="HX6050.X角度"
		export function HXmpu6050_get(parameter: any, block: any) {
				    let di = parameter.MP6.code;   
					Generator.addCode(`mpu6050.HXmpu6050_get(${di})`); 
		  }
		  
		  //% block="初始化显示屏" blockType="command"
		  export function SSD1306_init(parameter: any, block: any) {
		    			Generator.addInclude('SSD13061', '#include <U8g2lib.h>');
						Generator.addInclude('SSD13064', '#include <Wire.h>');
						Generator.addObject(`SSD13062`, `U8G2_SSD1306_128X64_NONAME_F_HW_I2C`, `u8g2(U8G2_R0,U8X8_PIN_NONE);`);
		    			Generator.addSetup('SSD13063', "delay(100);\n\tu8g2.begin();delay(100);\n\tu8g2.enableUTF8Print();");		    			
		    }
		//% block="在X:[ll]Y:[kk]显示[mm](字体高度:12)" blockType="command"
		 //% ll.shadow="number"   ll.defl="1"
		 //% kk.shadow="number"   kk.defl="1"
		 //% mm.shadow="string"   mm.defl="HXSY"
		 export function SSD1306_display(parameter: any, block: any) {
			   let dn = parameter.ll.code
			   let dny = parameter.kk.code
			   let sbu = parameter.mm.code
			   Generator.addCode(`u8g2.setFont(u8g2_font_wqy12_t_gb2312b);\n\tu8g2.setCursor(${dn},${dny}+10); \n\tu8g2.print(${sbu});\n\tu8g2.sendBuffer();`); 
		}
		//% block="清除X:[ll]Y:[kk]长度[mm](高度:12)" blockType="command"
		 //% ll.shadow="number"   ll.defl="1"
		 //% kk.shadow="number"   kk.defl="1"
		 //% mm.shadow="number"   mm.defl="1"
		 export function SSD1306_clear(parameter: any, block: any) {
			   let dn = parameter.ll.code
			   let dny = parameter.kk.code
			   let sbu = parameter.mm.code
			   Generator.addCode(`u8g2.setDrawColor(0);\n\tu8g2.drawBox(${dn},${dny},${sbu},12);\n\tu8g2.setDrawColor(1);\n\tu8g2.sendBuffer();`); 
		}
		//% block="显示屏清屏 "   blockType="command"
		 export function SSD1306_cle(parameter: any, block: any) {
			   Generator.addCode(`u8g2.clearBuffer();\n\tu8g2.sendBuffer();`); 
		}
		
		//% block="设置MP3的音量为[vol]" blockType="command"
		 //%vol.shadow="range"   vol.params.min=0    vol.params.max=30    vol.defl=20
		 export function HX_MP3_VOL(parameter: any, block: any) {
			   let vo = parameter.vol.code
			   Generator.addInclude('HXSY', '#include <HXSY.h>');
			   Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
			   Generator.addSetup('HXMP301', "Serial.begin(9600);");
			   Generator.addCode(`HX_block.HX_MP3_VOL(${vo});`);
			   }
	    //% block="MP3播放第[num]歌曲" blockType="command"
	    //% num.shadow="number"   num.defl="1"
	     export function HX_MP3_play(parameter: any, block: any) {
	    	   let vo = parameter.num.code
	    	   Generator.addInclude('HXSY', '#include <HXSY.h>');
	    	   Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
			   Generator.addSetup('HXMP301', "Serial.begin(9600);");
	    	   Generator.addCode(`HX_block.HX_MP3_play(${vo});`);
	    	   }
	    //% block="端口[DD]的按钮 [BUTTON]被按下?" blockType="boolean"
	    //% Flag.shadow="boolean"
	    //% BUTTON.shadow="dropdown" BUTTON.options="BTN" BUTTON.defl="BTN.1"
	    //% DD.shadow="dropdown" DD.options="DIG" BUTTON.defl="DIG.3"
	    export function buttonPressed(parameter: any, block: any) {
	           let dn = parameter.DD.code
	    	   let sbu = parameter.BUTTON.code
	    	   Generator.addInclude('HXSY', '#include <HXSY.h>');
	    	   Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
	           Generator.addCode([`HX_block.buttonPressed(${dn},${sbu})`,Generator.ORDER_UNARY_POSTFIX]);       
	    }
	    
	     //% block="等待端口[DD]的按钮 [BUTTON]被松开 " blockType="command"
	     //% BUTTON.shadow="dropdown" BUTTON.options="BTN" BUTTON.defl="BTN.1"
	     //% DD.shadow="dropdown" DD.options="DIG" BUTTON.defl="DIG.3"
	     export function waitbutton(parameter: any, block: any) {
	    	   let dn = parameter.DD.code
	    	   let sbu = parameter.BUTTON.code
	    	   Generator.addInclude('HXSY', '#include <HXSY.h>');
	    	   Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
	    	   Generator.addCode(`HX_block.waitbutton(${dn},${sbu});`); 
	    }
	    
	    //% block="数码管[DD]显示 [SEG] " blockType="command"
	    //% SEG.shadow="number"   SEG.defl="0000"
	    //% DD.shadow="dropdown" DD.options="DIG" DD.defl="DIG.3"
	    export function WriteSEG(parameter: any, block: any) {
	    	  let dn = parameter.DD.code
	    	  let SE = parameter.SEG.code
	    	  Generator.addInclude('HXSY', '#include <HXSY.h>');
	    	  Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
	    	  Generator.addCode(`HX_block.WriteSEG(${dn},${SE});`); 
	    }
	    
	      //% block="超声波端口[DD]设置[light]颜色2 [COLOR]" blockType="command"
	      //% COLOR.shadow="colorPalette" 
	      //% DD.shadow="dropdown" DD.options="DIG" DD.defl="DIG.3"
	      //% light.shadow="dropdown" light.options="LIGHTS" light.defl="LIGHTS.0"
	      export function HXsetColor(parameter: any, block: any) {
	             let color=parameter.COLOR.code;
	    		 let di = parameter.DD.code;
	    		 let as = parameter.light.code;
	    		 Generator.addInclude('HXSY', '#include <HXSY.h>');
	    		 Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
	    		 Generator.addCode(`HX_block.HXsetColor(${di},${as},${color});`); 
	        }
	    	
	    	//% block="超声波端口[DD]读取距离（cm）" blockType="reporter"
	    	//% DD.shadow="dropdown" DD.options="DIG" DD.defl="DIG.3"
	    	export function HXGETDISTENCE(parameter: any, block: any) {
	    				 let di = parameter.DD.code;
	    				 Generator.addInclude('HXSY', '#include <HXSY.h>');
	    				 Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
	    				 Generator.addCode(`HX_block.HXGETDISTENCE(${di})`); 
	    	  }
			//% block="电机 [SW]以[SP]速度开始启动" blockType="command"
			//% SW.shadow="dropdown" SW.options="MOTOR" SW.defl="MOTOR.0"
			//%SP.shadow="range"   SP.params.min=-100    SP.params.max=100    SP.defl=0
			export function MOTOR_SEND_START(parameter: any, block: any) {
				let mo = parameter.SW.code
				let ss = parameter.SP.code
				Generator.addInclude('HXSY', '#include <HXSY.h>');
				Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
				Generator.addSetup('WLWDJ', "HX_block.MOTOR_steup();");
				Generator.addCode(`HX_block.MOTOR_SEND_START(${mo},${ss});`);
			}
			//% block="停止电机 [SW]" blockType="command"
			//% SW.shadow="dropdown" SW.options="MOTOR" SW.defl="MOTOR.0"
			export function MOTOR_STOP_ONE(parameter: any, block: any) {
				let mo = parameter.SW.code
				Generator.addInclude('HXSY', '#include <HXSY.h>');
				Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
				Generator.addCode(`HX_block.MOTOR_STOP_ONE(${mo});`);
			}
			//% block="电机A:[SP1]B:[SP2]C:[SP3]D:[SP4]速度开始启动" blockType="command"
			//%SP1.shadow="range"   SP1.params.min=-100    SP1.params.max=100    SP1.defl=0
			//%SP2.shadow="range"   SP2.params.min=-100    SP2.params.max=100    SP2.defl=0
			//%SP3.shadow="range"   SP3.params.min=-100    SP3.params.max=100    SP3.defl=0
			//%SP4.shadow="range"   SP4.params.min=-100    SP4.params.max=100    SP4.defl=0
			export function MOTOR4_SEND_START(parameter: any, block: any) {
				let s1 = parameter.SP1.code
				let s2 = parameter.SP2.code
				let s3 = parameter.SP3.code
				let s4 = parameter.SP4.code
				
				Generator.addInclude('HXSY', '#include <HXSY.h>');
				Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
				Generator.addSetup('WLWDJ', "HX_block.MOTOR_steup();");
				Generator.addCode(`HX_block.MOTOR4_SEND_START(${s1},${s2},${s3},${s4});`);
			}
			//% block="停止全部电机" blockType="command"
			export function MOTOR_STOP(parameter: any, block: any) {
				Generator.addInclude('HXSY', '#include <HXSY.h>');
				Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
				Generator.addCode(`HX_block.MOTOR_STOP();`);
			}
			
			//% block="寻迹[DD]的[light]检测到[color]色 " blockType="boolean"
			 //% Flag.shadow="boolean"
			 //% light.shadow="dropdown" light.options="LIGHTS" light.defl="LIGHTS.0"
			 //% color.shadow="dropdown" color.options="COLORS" color.defl="COLORS.1"
			 //% DD.shadow="dropdown" DD.options="DIG" DD.defl="DIG.3"
			 export function LIGHT_B_W(parameter: any, block: any) {
				   let dn = parameter.DD.code
				   let li = parameter.light.code
				   let col = parameter.color.code
				   Generator.addInclude('HXSY', '#include <HXSY.h>');
				   Generator.addObject(`HX_block`, `HXSY`, `HX_block;`);
				   Generator.addCode(`HX_block.LIGHT_B_W(${dn},${li},${col})`); 
			}
			
			
		
 
}
